package com.yunke.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.*;
import com.yunke.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 管理员端扩展功能控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/extension")
@RequiredArgsConstructor
@Tag(name = "管理员端扩展功能", description = "管理员端扩展功能管理相关接口")
public class AdminExtensionController {

    private final YunkeTagService yunkeTagService;
    private final YunkeFileService yunkeFileService;
    private final YunkeScheduleJobService yunkeScheduleJobService;
    private final YunkeSmsService yunkeSmsService;
    private final YunkeContactApplyService yunkeContactApplyService;

    // ============ 标签管理 ============
    
    @PostMapping("/tag")
    @Operation(summary = "创建标签")
    public YunkeResult<String> createTag(@Valid @RequestBody YunkeTag tag) {
        boolean success = yunkeTagService.createTag(tag);
        return success ? YunkeResult.success("创建成功") : YunkeResult.error("创建失败");
    }

    @PutMapping("/tag/{id}")
    @Operation(summary = "更新标签")
    public YunkeResult<String> updateTag(@PathVariable Long id, @Valid @RequestBody YunkeTag tag) {
        tag.setId(id);
        boolean success = yunkeTagService.updateTag(tag);
        return success ? YunkeResult.success("更新成功") : YunkeResult.error("更新失败");
    }

    @DeleteMapping("/tag/{id}")
    @Operation(summary = "删除标签")
    public YunkeResult<String> deleteTag(@PathVariable Long id) {
        boolean success = yunkeTagService.deleteTag(id);
        return success ? YunkeResult.success("删除成功") : YunkeResult.error("删除失败");
    }

    @PostMapping("/tag/delete/batch")
    @Operation(summary = "批量删除标签")
    public YunkeResult<String> deleteTags(@RequestBody List<Long> ids) {
        boolean success = yunkeTagService.deleteTags(ids);
        return success ? YunkeResult.success("批量删除成功") : YunkeResult.error("批量删除失败");
    }

    @GetMapping("/tag/page")
    @Operation(summary = "分页查询标签列表")
    public YunkeResult<IPage<YunkeTag>> getTagPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String tagName,
            @RequestParam(required = false) Integer tagType,
            @RequestParam(required = false) String tagCategory,
            @RequestParam(required = false) Boolean isEnabled) {
        IPage<YunkeTag> page = yunkeTagService.getTagPage(current, size, tagName, tagType, tagCategory, isEnabled);
        return YunkeResult.success(page);
    }

    @PostMapping("/tag/enable/{id}")
    @Operation(summary = "启用标签")
    public YunkeResult<String> enableTag(@PathVariable Long id) {
        boolean success = yunkeTagService.enableTag(id);
        return success ? YunkeResult.success("启用成功") : YunkeResult.error("启用失败");
    }

    @PostMapping("/tag/disable/{id}")
    @Operation(summary = "禁用标签")
    public YunkeResult<String> disableTag(@PathVariable Long id) {
        boolean success = yunkeTagService.disableTag(id);
        return success ? YunkeResult.success("禁用成功") : YunkeResult.error("禁用失败");
    }

    @PostMapping("/tag/hot/{id}")
    @Operation(summary = "设为热门")
    public YunkeResult<String> setHotTag(@PathVariable Long id) {
        boolean success = yunkeTagService.setHot(id);
        return success ? YunkeResult.success("设置成功") : YunkeResult.error("设置失败");
    }

    @PostMapping("/tag/recommend/{id}")
    @Operation(summary = "设为推荐")
    public YunkeResult<String> setRecommendTag(@PathVariable Long id) {
        boolean success = yunkeTagService.setRecommend(id);
        return success ? YunkeResult.success("设置成功") : YunkeResult.error("设置失败");
    }

    // ============ 文件管理 ============

    @GetMapping("/file/page")
    @Operation(summary = "分页查询文件列表")
    public YunkeResult<IPage<YunkeFile>> getFilePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer fileType,
            @RequestParam(required = false) String fileCategory,
            @RequestParam(required = false) String businessType,
            @RequestParam(required = false) Long uploadUserId) {
        IPage<YunkeFile> page = yunkeFileService.getFilePage(current, size, fileType, fileCategory, businessType, uploadUserId);
        return YunkeResult.success(page);
    }

    @DeleteMapping("/file/{id}")
    @Operation(summary = "删除文件")
    public YunkeResult<String> deleteFile(@PathVariable Long id) {
        // 管理员可以删除任意文件
        boolean success = yunkeFileService.deleteFile(id, null);
        return success ? YunkeResult.success("删除成功") : YunkeResult.error("删除失败");
    }

    @PostMapping("/file/clean/temp")
    @Operation(summary = "清理临时文件")
    public YunkeResult<String> cleanTempFiles() {
        boolean success = yunkeFileService.cleanTempFiles();
        return success ? YunkeResult.success("清理成功") : YunkeResult.error("清理失败");
    }

    @PostMapping("/file/clean/expired")
    @Operation(summary = "清理过期文件")
    public YunkeResult<String> cleanExpiredFiles() {
        boolean success = yunkeFileService.cleanExpiredFiles();
        return success ? YunkeResult.success("清理成功") : YunkeResult.error("清理失败");
    }

    @GetMapping("/file/statistics")
    @Operation(summary = "文件统计")
    public YunkeResult<Map<String, Object>> getFileStatistics() {
        // 实现文件统计逻辑
        // TODO: 调用统计服务获取文件统计数据
        return YunkeResult.success(null);
    }

    // ============ 定时任务管理 ============

    @PostMapping("/job")
    @Operation(summary = "创建定时任务")
    public YunkeResult<String> createJob(@Valid @RequestBody YunkeScheduleJob job) {
        boolean success = yunkeScheduleJobService.createJob(job);
        return success ? YunkeResult.success("创建成功") : YunkeResult.error("创建失败");
    }

    @PutMapping("/job/{id}")
    @Operation(summary = "更新定时任务")
    public YunkeResult<String> updateJob(@PathVariable Long id, @Valid @RequestBody YunkeScheduleJob job) {
        job.setId(id);
        boolean success = yunkeScheduleJobService.updateJob(job);
        return success ? YunkeResult.success("更新成功") : YunkeResult.error("更新失败");
    }

    @DeleteMapping("/job/{id}")
    @Operation(summary = "删除定时任务")
    public YunkeResult<String> deleteJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.deleteJob(id);
        return success ? YunkeResult.success("删除成功") : YunkeResult.error("删除失败");
    }

    @GetMapping("/job/page")
    @Operation(summary = "分页查询定时任务列表")
    public YunkeResult<IPage<YunkeScheduleJob>> getJobPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String jobName,
            @RequestParam(required = false) String jobGroup,
            @RequestParam(required = false) Integer jobStatus) {
        IPage<YunkeScheduleJob> page = yunkeScheduleJobService.getJobPage(current, size, jobName, jobGroup, jobStatus);
        return YunkeResult.success(page);
    }

    @PostMapping("/job/start/{id}")
    @Operation(summary = "启动定时任务")
    public YunkeResult<String> startJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.startJob(id);
        return success ? YunkeResult.success("启动成功") : YunkeResult.error("启动失败");
    }

    @PostMapping("/job/stop/{id}")
    @Operation(summary = "停止定时任务")
    public YunkeResult<String> stopJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.stopJob(id);
        return success ? YunkeResult.success("停止成功") : YunkeResult.error("停止失败");
    }

    @PostMapping("/job/pause/{id}")
    @Operation(summary = "暂停定时任务")
    public YunkeResult<String> pauseJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.pauseJob(id);
        return success ? YunkeResult.success("暂停成功") : YunkeResult.error("暂停失败");
    }

    @PostMapping("/job/resume/{id}")
    @Operation(summary = "恢复定时任务")
    public YunkeResult<String> resumeJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.resumeJob(id);
        return success ? YunkeResult.success("恢复成功") : YunkeResult.error("恢复失败");
    }

    @PostMapping("/job/execute/{id}")
    @Operation(summary = "立即执行定时任务")
    public YunkeResult<String> executeJob(@PathVariable Long id) {
        boolean success = yunkeScheduleJobService.executeJob(id);
        return success ? YunkeResult.success("执行成功") : YunkeResult.error("执行失败");
    }

    // ============ 短信管理 ============

    @GetMapping("/sms/page")
    @Operation(summary = "分页查询短信列表")
    public YunkeResult<IPage<YunkeSms>> getSmsPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String phoneNumber,
            @RequestParam(required = false) Integer smsType,
            @RequestParam(required = false) Integer sendStatus) {
        IPage<YunkeSms> page = yunkeSmsService.getSmsPage(current, size, phoneNumber, smsType, sendStatus);
        return YunkeResult.success(page);
    }

    @GetMapping("/sms/statistics")
    @Operation(summary = "短信统计")
    public YunkeResult<Map<String, Object>> getSmsStatistics() {
        Map<String, Object> statistics = yunkeSmsService.getSmsStatistics();
        return YunkeResult.success(statistics);
    }

    @PostMapping("/sms/resend/{id}")
    @Operation(summary = "重新发送短信")
    public YunkeResult<String> resendSms(@PathVariable Long id) {
        boolean success = yunkeSmsService.resendSms(id);
        return success ? YunkeResult.success("重发成功") : YunkeResult.error("重发失败");
    }

    // ============ 联系方式申请管理 ============

    @GetMapping("/contact-apply/page")
    @Operation(summary = "分页查询联系方式申请列表")
    public YunkeResult<IPage<YunkeContactApply>> getContactApplyPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long applyUserId,
            @RequestParam(required = false) Long targetUserId,
            @RequestParam(required = false) Integer applyStatus) {
        IPage<YunkeContactApply> page = yunkeContactApplyService.getApplyPage(current, size, applyUserId, targetUserId, applyStatus);
        return YunkeResult.success(page);
    }

    @GetMapping("/contact-apply/statistics")
    @Operation(summary = "联系方式申请统计")
    public YunkeResult<Map<String, Object>> getContactApplyStatistics() {
        Map<String, Object> statistics = yunkeContactApplyService.getApplyStatistics();
        return YunkeResult.success(statistics);
    }

    @PostMapping("/contact-apply/force-approve/{id}")
    @Operation(summary = "强制同意申请")
    public YunkeResult<String> forceApproveApply(@PathVariable Long id) {
        boolean success = yunkeContactApplyService.forceApproveApply(id);
        return success ? YunkeResult.success("操作成功") : YunkeResult.error("操作失败");
    }

    @PostMapping("/contact-apply/force-reject/{id}")
    @Operation(summary = "强制拒绝申请")
    public YunkeResult<String> forceRejectApply(@PathVariable Long id, @RequestParam String reason) {
        boolean success = yunkeContactApplyService.forceRejectApply(id, reason);
        return success ? YunkeResult.success("操作成功") : YunkeResult.error("操作失败");
    }
} 